package com.hongxun.budget.ui.fragment.tk;

import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.DMaterialCostEntityDao;
import com.hongxun.budget.greendao.DOtherCostEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.entity.sg.CrossInfo;
import com.hongxun.greenentity.tk.DMaterialCostEntity;
import com.hongxun.greenentity.tk.DOtherCostEntity;
import com.hongxun.greenentity.tk.DPointEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/8/17.
 */

public class QuantitiesInfoFragment extends BaseListFragment {


    List<CrossInfo> datas = new ArrayList<>();
    SuperAdapter<CrossInfo> adapter;

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<CrossInfo>(R.layout.content_fragment_quantities_info) {
            @Override
            public void bindView(ViewHolder viewHolder, CrossInfo d, int i) {
                ((TextView) viewHolder.getView(R.id.lineName)).setText(d.getLineName());
                ((TextView) viewHolder.getView(R.id.price)).setText((d.getPrice() == null ? 0 : d.getPrice()) + "元");
                ((TextView) viewHolder.getView(R.id.name)).setText(d.getName());
                ((TextView) viewHolder.getView(R.id.parameter)).setText(d.getDeviceName());
                ((TextView) viewHolder.getView(R.id.count)).setText(d.getCount() + "");
                ((TextView) viewHolder.getView(R.id.unit)).setText(d.getUnit());
                ((TextView) viewHolder.getView(R.id.type)).setText(d.getType() == 0 ? "设备:   " : (d.getType() == 3 ? "点位:   " : "材料:   "));
            }
        };
    }

    @Override
    public  void initView() {
        super.initView();
        initData();
        adapter.setData(datas);
        adapter.setEmptyDataView(R.layout.view_empty);
    }

    void initData() {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<DOtherCostEntity> buyEntities = session.getDOtherCostEntityDao().queryBuilder().where(
                DOtherCostEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                , DOtherCostEntityDao.Properties.CCDatastate.notEq("删除")
        ).list();
        for (DOtherCostEntity d : buyEntities) {
            CrossInfo m = new CrossInfo();
            m.setCount(d.getOQCount());
            m.setLineName(DBUtils.getLineNameWithPPname(d.getPPGuid()));
            m.setName(d.getOCName());
            m.setUnit(d.getOCMeterUnit());
            m.setPrice(d.getOCPriceall());
            DPointEntity load = session.getDPointEntityDao().load(d.getPPGuid());
            m.setDeviceName(load == null ? "交叉跨越" : (load.getPPCode() + "(" + load.getPPName() + ")"));
            m.setType(3);
            datas.add(m);
        }
        List<DMaterialCostEntity> list = session.getDMaterialCostEntityDao().queryBuilder().where(
                DMaterialCostEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", ""))
                , DMaterialCostEntityDao.Properties.MCDatastate.notEq("删除")
        ).list();
        for (DMaterialCostEntity d : list) {
            CrossInfo m = new CrossInfo();
            m.setCount(d.getMCCount());
            m.setLineName(DBUtils.getLineNameWithPPname(d.getPPGuid()));
            m.setName(d.getMCQuantities());
            m.setUnit(d.getMCUnit());
            m.setPrice(d.getMCPriceall());
            m.setDeviceName(d.getMMName());
            m.setType(d.getMMType() == null ? 0 : d.getMMType());
            datas.add(m);
        }
    }

    ;
}
